package com.ss.android.article.base.feature.feed.presenter;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.b.ThreadPlus;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.common.utility.collection.WeakHandler.Callback;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.category.a.CategoryManager;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.image.BaseImageManager;
import com.ss.android.image.model.ImageInfo;
import com.ss.android.newmedia.util.AppUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public final class RefreshAdManager implements Callback, CategoryManager.a$a {
    public static final class o$a {
        public long mCreativeId;
        public ImageInfo mImageInfo;
        public String c;
        public boolean d;
        public final ArrayList e;
        public long f;
        public long g;
        public List h;
        public List i;
        public final List j;

        public o$a() {
            this.e = new ArrayList();
            this.h = new ArrayList();
            this.i = new ArrayList();
            this.j = new ArrayList();
        }

        public final boolean a() {
            boolean v0 = this.mCreativeId < 0 || this.mImageInfo == null ? false : true;
            return v0;
        }

        public final void a(JSONObject arg7) {
            String v4;
            int v3;
            int v1 = 0;
            this.mCreativeId = arg7.optLong("creative_id");
            this.mImageInfo = ImageInfo.fromJson(arg7.optJSONObject("image_info"), false);
            this.c = arg7.optString("log_extra");
            boolean v0 = arg7.optInt("is_day_mode") == 0 ? true : false;
            this.d = v0;
            JSONArray v2 = arg7.optJSONArray("channel");
            if (v2 != null && v2.length() > 0) {
                v3 = v2.length();
                int v0_1;
                for (v0_1 = 0; v0_1 < v3; ++v0_1) {
                    v4 = v2.optString(v0_1);
                    if (v4 != null && !android.support.design.a.isTextEmpty(v4)) {
                        this.e.add(v4);
                    }
                }
            }

            arg7.optString("image_type");
            this.f = arg7.optLong("start_time");
            this.g = arg7.optLong("end_time");
            v2 = arg7.optJSONArray("shown_track_url");
            if (v2 != null && v2.length() > 0) {
                v3 = v2.length();
                for (int v0_1 = 0; v0_1 < v3; ++v0_1) {
                    v4 = v2.optString(v0_1);
                    if (v4 != null && !android.support.design.a.isTextEmpty(v4)) {
                        this.h.add(v4);
                    }
                }
            }

            v2 = arg7.optJSONArray("fully_shown_track_url");
            if (v2 != null && v2.length() > 0) {
                v3 = v2.length();
                for (int v0_1 = 0; v0_1 < v3; ++v0_1) {
                    v4 = v2.optString(v0_1);
                    if (v4 != null && !android.support.design.a.isTextEmpty(v4)) {
                        this.i.add(v4);
                    }
                }
            }

            JSONArray v0_2 = arg7.optJSONArray("interval_ads");
            if (v0_2 != null && v0_2.length() > 0) {
                int v2_1 = v0_2.length();
                while (v1 < v2_1) {
                    JSONObject v3_1 = v0_2.optJSONObject(v1);
                    if (v3_1 != null) {
                        o$a v4_1 = new o$a();
                        v4_1.a(v3_1);
                        if (v4_1.a()) {
                            this.j.add(v4_1);
                        }
                    }

                    ++v1;
                }
            }
        }
    }

    public static final class b {
        public final List a;

        public b() {

            this.a = new ArrayList();
        }

        public final void a(JSONArray arg5) {
            int v0_1;
            if (arg5 != null) {
                try {
                    int v1 = arg5.length();
                    v0_1 = 0;
                    while (true) {
                        if (v0_1 >= v1) {
                            return;
                        }

                        JSONObject v2 = arg5.optJSONObject(v0_1);
                        if (v2 != null) {
                            o$a v3 = new o$a();
                            v3.a(v2);
                            if (v3.a()) {
                                this.a.add(v3);
                            }
                        }

                        ++v0_1;
                        continue;
                    }
                } catch (Exception v0) {
                    return;
                }
            }
        }
    }

    static class c extends Thread {
        private c() {

        }

        c(byte arg1) {
            this();
        }

        public void a() {
        }

        public final void run() {
            try {
                this.a();
            } catch (Throwable v0) {
            }
        }
    }

    static final Object mLock = new Object();
    Context mContext;
    final SharedPreferences mSharedPreferences;
    final WeakHandler mWeakHandler;
    long mLastFetchTime;
    private static RefreshAdManager sRefreshAdManager = null;
    private AppData mAppData;
    private boolean h;
    private b i;

    private RefreshAdManager(Context context) {
        this.mWeakHandler = new WeakHandler(Looper.getMainLooper(), this);
        this.mLastFetchTime = 0;
        this.h = false;
        this.i = new b();
        this.mContext = context.getApplicationContext();
        this.mSharedPreferences = this.mContext.getSharedPreferences("ss_refresh_ad", 0);
        this.mAppData = AppData.getInstance();
        if (!this.mAppData.refreshAdDisable && !this.h) {
            this.h = true;
            String pullRefreshAdStr;
            synchronized (RefreshAdManager.mLock) {
                pullRefreshAdStr = this.mSharedPreferences.getString("pull_refresh_ad_str", "");
                this.mLastFetchTime = this.mSharedPreferences.getLong("last_fetch_time", 0);
            }

            if (!android.support.design.a.isTextEmpty(pullRefreshAdStr)) {
                b v0_2 = RefreshAdManager.b(pullRefreshAdStr);
                if (v0_2 != null) {
                    this.i = v0_2;
                    this.a(v0_2);
                }
            }
        }
    }

    public final void a(boolean arg7) {
        if (!this.mAppData.refreshAdDisable && (NetworkUtils.isNetworkAvailable(this.mContext))) {
            long v0 = 0;
            if (!arg7) {
                v0 = this.mAppData.refreshAdExpireSec;
                if (v0 > 600) {
                    v0 *= 1000;
                } else {
                    v0 = 43200000;
                }
            }

            if (System.currentTimeMillis() - this.mLastFetchTime <= v0) {
                return;
            }
            new ThreadPlus(new p(this), "pull_refresh_ad_manager", true).start();
        }
    }

    public static synchronized RefreshAdManager a(Context arg2) {
        if (RefreshAdManager.sRefreshAdManager == null) {
            RefreshAdManager.sRefreshAdManager = new RefreshAdManager(arg2);
        }
        return RefreshAdManager.sRefreshAdManager;
    }

    public static void a(Context context, o$a arg10, String arg11) {
        if (context != null && arg10 != null) {
            JSONObject v8 = new JSONObject();
            try {
                if (!android.support.design.a.isTextEmpty(arg10.c)) {
                    v8.put("log_extra", arg10.c);
                }

                if (android.support.design.a.isTextEmpty(arg11)) {
                } else {
                    v8.put("chn_id", arg11);
                }
            } catch (Exception v0) {
                v8 = null;
            }

            MobClickCombiner.onEvent(context, "refresh_ad", "show", arg10.mCreativeId, 0, v8);
            AppUtil.a(arg10.h, context);
        }
    }

    public final o$a a(String arg13) {
        Object v9_1 = null;
        Object v3 = null;
        Object v8;
        o$a v9 = null;
        if (!this.mAppData.refreshAdDisable && (!android.support.design.a.isTextEmpty(arg13) && this.i != null)) {
            long v4 = System.currentTimeMillis();
            boolean v6 = AppData.isNightMode();
            b v2 = this.i;
            try {
                Iterator v10 = v2.a.iterator();
                do {
                    if (!v10.hasNext()) {
                        break;
                    }

                    v8 = v10.next();
                    if (v8 != null && !((o$a) v8).j.isEmpty()) {
                        Iterator v11 = ((o$a) v8).j.iterator();
                        do {
                            if (v11.hasNext()) {
                                v3 = v11.next();
                                if (v3 == null) {
                                    continue;
                                }

                                if (!((o$a) v3).a()) {
                                    continue;
                                }

                                if (!this.a(((o$a) v3), v4, v6, arg13)) {
                                    continue;
                                }
                            }
                            break;
                        }
                        while (true);
                        v9_1 = v3;
                        return (o$a) v9_1;
                    }

                    if (!this.a((o$a) v8, v4, v6, arg13)) {
                        continue;
                    }
                    return (o$a) v8;
                }
                while (true);
            } catch (Exception v2_1) {
            }

            v8 = v9_1;
            v9_1 = v8;
        }
        return ((o$a) v9_1);
    }

    public static void a(Context arg9, o$a arg10, String arg11, int arg12) {
        if (arg9 != null && arg10 != null) {
            JSONObject v8 = new JSONObject();
            try {
                if (!android.support.design.a.isTextEmpty(arg10.c)) {
                    v8.put("log_extra", arg10.c);
                }

                if (!android.support.design.a.isTextEmpty(arg11)) {
                    v8.put("chn_id", arg11);
                }

                if (arg12 <= 0) {
                } else {
                    v8.put("pct", arg12);
                }
            } catch (Exception v0) {
                v8 = null;
            }
            MobClickCombiner.onEvent(arg9, "refresh_ad", "show_done", arg10.mCreativeId, 0, v8);
        }
    }

    private void a(b arg4) {
        if (arg4 != null && (NetworkUtils.isNetworkAvailable(this.mContext))) {
            try {
                BaseImageManager imageManager = new BaseImageManager(this.mContext);
                if (!BaseImageManager.isExternalStorageMounted()) {
                    return;
                }

                List v1 = arg4.a;
                if (v1.isEmpty()) {
                    return;
                }

                new q(this, v1, imageManager).start();
            } catch (Exception v0) {
            }
        }
    }

    private boolean a(o$a arg9, long arg10, boolean arg12, String arg13) {
        long v6 = 1000;
        boolean v0 = false;
        if (arg9 != null && !android.support.design.a.isTextEmpty(arg13) && arg9.d == arg12 && arg10 / v6 <= arg9.g && arg10 / v6 >= arg9.f && ((arg9.e.isEmpty()) || (arg9.e.contains(arg13)))) {
            ImageInfo v1 = arg9.mImageInfo;
            if (v1 == null) {
                return v0;
            }

            if (android.support.design.a.isTextEmpty(v1.mKey)) {
                return v0;
            }

            String v1_1 = v1.mKey;
            if (android.support.design.a.isTextEmpty(v1_1)) {
                return v0;
            }

            v0 = new BaseImageManager(this.mContext).existsInExternalCache(v1_1);
        }

        return v0;
    }

    public final boolean a(Context arg6, o$a arg7, ImageView arg8) {
        String v1_2;
        int v2 = View.GONE;
        boolean v0 = false;
        if (!this.mAppData.refreshAdDisable && arg6 != null && arg7 != null && arg8 != null) {
            int v1 = View.GONE;
            try {
                arg8.setVisibility(v1);
                if (arg7.mImageInfo != null) {
                    v1_2 = arg7.mImageInfo.mKey;
                } else {
                    v1_2 = null;
                }
                if (android.support.design.a.isTextEmpty(v1_2)) {
                    return v0;
                }

                BaseImageManager v2_1 = new BaseImageManager(arg6);
                if (!BaseImageManager.a()) {
                    return v0;
                }

                Bitmap v1_3 = v2_1.a(v1_2, 750, 750);
                if (v1_3 == null) {
                    return v0;
                }

                arg8.setImageBitmap(v1_3);
                arg8.setVisibility(View.VISIBLE);
                v0 = true;
            } catch (Exception v1_1) {
                Logger.d("RefreshAdManager", "show AD FAIL exception: " + v1_1);
            }
        } else {
            arg8.setVisibility(v2);
        }

        return v0;
    }

    final boolean a(BaseImageManager arg12, ImageInfo arg13) {
        String v6;
        String v5;
        String v4;
        boolean v10 = true;
        boolean v9 = false;
        if (arg13 == null) {
            return v9;
        }

        try {
            String v0_1 = arg13.mKey;
            v4 = arg12.getExternalImageSaveDir(v0_1);
            v5 = arg12.getInternalImageSaveDir(v0_1);
            v6 = BaseImageManager.getCacheFileName(v0_1);
            if (!arg12.existsInExternalCache(v0_1)) {
                Context v0_3 = null;
                int v1 = -1;
                try {
                    v9 = AppUtil.downloadImage(v0_3, v1, arg13.mUri, arg13.mUrlList, v4, v5, v6, null, null);
                } catch (Throwable v0) {
                    if (AppUtil.a(TTUtils.getErrorCode(this.mContext, v0))) {
                    } else {
                        v10 = v9;
                    }

                }
                boolean v0_2;
                if (!v9) {
                    if (!v10) {
                        return v9;
                    }

                    v0_3 = null;
                    v1 = -1;
                    try {
                        v0_2 = AppUtil.downloadImage(v0_3, v1, arg13.mUri, arg13.mUrlList, v4, v5, v6, null, null);
                    } catch (Throwable v0) {
                        v0_2 = v9;
                    }
                } else {
                    v0_2 = v9;
                }
                return v0_2;
            } else {
                return true;
            }
        } catch (Throwable v0) {
        }

        boolean v0_2 = true;
        return v0_2;

    }

    public static void b(Context arg9, o$a arg10, String arg11) {
        if (arg9 != null && arg10 != null) {
            JSONObject v8 = new JSONObject();
            try {
                if (!android.support.design.a.isTextEmpty(arg10.c)) {
                    v8.put("log_extra", arg10.c);
                }

                if (android.support.design.a.isTextEmpty(arg11)) {
                } else {
                    v8.put("chn_id", arg11);
                }
            } catch (Exception v0) {
                v8 = null;
            }

            MobClickCombiner.onEvent(arg9, "refresh_ad", "show_full", arg10.mCreativeId, 0, v8);
            AppUtil.a(arg10.i, arg9);
        }
    }

    private static b b(String arg2) {
        b v0 = new b();
        if (android.support.design.a.isTextEmpty(arg2)) {
            return null;
        }

        try {
            v0.a(new JSONArray(arg2));
        } catch (Exception v1) {
        }

        return v0;
    }

    public final void c(boolean arg1) {
        if (arg1) {
            this.a(arg1);
        }
    }

    public final void handleMsg(Message message) {
        switch (message.what) {
            case 10: {
                if ((message.obj instanceof b)) {
                    Object v0 = message.obj;
                    if (v0 != null) {
                        this.i = ((b) v0);
                        this.a(this.i);
                    }
                }

                break;
            }
        }
    }
}

